<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>semop</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_008_375">&nbsp;</a>NAME</h4><blockquote>
semop - semaphore operations
</blockquote><h4><a name = "tag_000_008_376">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="syssem.h.html">sys/sem.h</a>&gt;

int semop(int <i>semid</i>, struct sembuf *<i>sops</i>, size_t <i>nsops</i>);
</code>
</pre>
</blockquote><h4><a name = "tag_000_008_377">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>semop()</i>
function
is used to perform atomically a user-defined array
of semaphore operations on the set of semaphores
associated with the semaphore identifier specified by the
argument
<i>semid</i>.
<p>
The argument
<i>sops</i>
is a pointer to a user-defined array of semaphore operation structures.
The implementation will not modify elements of this array unless
the application uses implementation-dependent extensions.
<p>
The argument
<i>nsops</i>
is the number of such structures in the array.
<p>
Each structure,
<b>sembuf</b>,
includes the following members:
<p><table  bordercolor=#000000 border=1 align=center><tr valign=top><th align=center><b>Member Type</b>
<th align=center><b>Member Name</b>
<th align=center><b>Description</b>
<tr valign=top><td align=left>short
<td align=left>sem_num
<td align=left>semaphore number
<tr valign=top><td align=left>short
<td align=left>sem_op
<td align=left>semaphore operation
<tr valign=top><td align=left>short
<td align=left>sem_flg
<td align=left>operation flags
</table>
<p>
Each semaphore operation specified by
<i>sem_op</i>
is performed on the corresponding semaphore specified by
<i>semid</i>
and
<i>sem_num</i>.
<p>
The variable
<i>sem_op</i>
specifies one of three semaphore operations:
<ol>
<p>
<li>
If
<i>sem_op</i>
is a negative integer and the calling process
has alter permission, one of the following will occur:
<ul>
<p>
<li>
If
<i>semval</i>,
see
<i><a href="syssem.h.html">&lt;sys/sem.h&gt;</a></i>,
is greater
than or equal to the absolute value of
<i>sem_op</i>,
the absolute value of
<i>sem_op</i>
is subtracted from
<i>semval</i>.
Also, if (<i>sem_flg</i>&amp;SEM_UNDO) is non-zero, the absolute value of
<i>sem_op</i>
is added to the calling process'
<i>semadj</i>
value for the specified semaphore.
<p>
<li>
If
<i>semval</i>
is less than the absolute value of
<i>sem_op</i>
and (<i>sem_flg</i>&amp;IPC_NOWAIT) is non-zero,
<i>semop()</i>
will return immediately.
<br>
<p>
<li>
If
<i>semval</i>
is less than the absolute value of
<i>sem_op</i>
and (<i>sem_flg</i>&amp;IPC_NOWAIT) is 0,
<i>semop()</i>
will increment the
<i>semncnt</i>
associated with the specified semaphore and suspend execution
of the calling thread until one of the following conditions occurs:
<ul>
<p>
<li>
The value of
<i>semval</i>
becomes greater than or equal to the absolute value of
<i>sem_op</i>.
When this occurs, the value of
<i>semncnt</i>
associated with the specified semaphore is decremented, the
absolute value of
<i>sem_op</i>
is subtracted from
<i>semval</i>
and, if (<i>sem_flg</i>&amp;SEM_UNDO) is non-zero, the absolute value of
<i>sem_op</i>
is added to the calling process'
<i>semadj</i>
value for the specified semaphore.
<br>
<p>
<li>
The
<i>semid</i>
for which the calling thread is awaiting action is removed from the system.
When this occurs,
<i>errno</i>
is set equal to [EIDRM] and -1 is returned.
<br>
<p>
<li>
The calling thread receives a signal that is to be caught.
When this occurs, the value of
<i>semncnt</i>
associated with the specified semaphore is decremented,
and the calling thread resumes execution in the manner
prescribed in
<i><a href="sigaction.html">sigaction()</a></i>.
<p>
</ul>
<p>
</ul>
<p>
<li>
If
<i>sem_op</i>
is a positive integer and the calling process has alter permission,
the value of
<i>sem_op</i>
is added to
<i>semval</i>
and, if (<i>sem_flg</i>&amp;SEM_UNDO) is non-zero, the value of
<i>sem_op</i>
is subtracted from the calling process'
<i>semadj</i>
value for the specified semaphore.
<p>
<li>
If
<i>sem_op</i>
is 0 and the calling process has read permission,
one of the following will occur:
<ul>
<p>
<li>
If
<i>semval</i>
is 0,
<i>semop()</i>
will return immediately.
<p>
<li>
If
<i>semval</i>
is non-zero and (<i>sem_flg</i>&amp;IPC_NOWAIT) is non-zero,
<i>semop()</i>
will return immediately.
<p>
<li>
If
<i>semval</i>
is non-zero and (<i>sem_flg</i>&amp;IPC_NOWAIT) is 0,
<i>semop()</i>
will increment the
<i>semzcnt</i>
associated with the specified semaphore and suspend execution
of the calling thread until one of the following occurs:
<ul>
<p>
<li>
The value of
<i>semval</i>
becomes 0, at which time the value of
<i>semzcnt</i>
associated with the specified semaphore is decremented.
<p>
<li>
The
<i>semid</i>
for which the calling thread is awaiting action is
removed from the system.
When this occurs,
<i>errno</i>
is set equal to [EIDRM] and -1 is returned.
<p>
<li>
The calling thread receives a signal that is to be caught.
When this occurs, the value of
<i>semzcnt</i>
associated with the specified semaphore is decremented,
and the calling thread resumes execution in the manner
prescribed in
<i><a href="sigaction.html">sigaction()</a></i>.
<p>
</ul>
<p>
</ul>
<p>
</ol>
<p>
Upon successful completion, the value of
<i>sempid</i>
for each semaphore specified in the array pointed to by
<i>sops</i>
is set equal to the process ID of the calling process.
</blockquote><h4><a name = "tag_000_008_378">&nbsp;</a>RETURN VALUE</h4><blockquote>
Upon successful completion,
<i>semop()</i>
returns 0. Otherwise, it returns -1 and
<i>errno</i>
will be set to indicate the error.
<br>
</blockquote><h4><a name = "tag_000_008_379">&nbsp;</a>ERRORS</h4><blockquote>
The
<i>semop()</i>
function will fail if:
<dl compact>

<dt>[E2BIG]<dd>
The value of
<i>nsops</i>
is greater than the system-imposed maximum.

<dt>[EACCES]<dd>
Operation permission is denied to the calling process, see
<a href="ipc.html">IPC</a>.


<dt>[EAGAIN]<dd>
The operation would result in suspension of the
calling process but (<i>sem_flg</i>&amp;IPC_NOWAIT) is non-zero.

<dt>[EFBIG]<dd>
The value of
<i>sem_num</i>
is less than 0 or greater than or equal to the
number of semaphores in the set associated with
<i>semid</i>.

<dt>[EIDRM]<dd>
The semaphore identifier
<i>semid</i>
is removed from the system.

<dt>[EINTR]<dd>
The
<i>semop()</i>
function was interrupted by a signal.


<dt>[EINVAL]<dd>
The value of
<i>semid</i>
is not a valid semaphore identifier,
or the number of individual semaphores for which
the calling process requests a SEM_UNDO
would exceed the system-imposed limit.


<dt>[ENOSPC]<dd>
The limit on the number of individual processes requesting a SEM_UNDO
would be exceeded.

<dt>[ERANGE]<dd>
An operation would cause a
<i>semval</i>
to overflow the system-imposed limit, or an operation
would cause a
<i>semadj</i>
value to overflow the system-imposed limit.

</dl>
</blockquote><h4><a name = "tag_000_008_380">&nbsp;</a>EXAMPLES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_008_381">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
The POSIX Realtime Extension defines alternative interfaces for interprocess communication.
Application developers who need to use IPC should design their
applications so that modules using the IPC routines described in
<a href="ipc.html">IPC</a>
can be easily modified to use the alternative interfaces.
</blockquote><h4><a name = "tag_000_008_382">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_008_383">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="exec.html">exec</a></i>,
<i><a href="exit.html">exit()</a></i>,
<i><a href="fork.html">fork()</a></i>,
<i><a href="semctl.html">semctl()</a></i>,
<i><a href="semget.html">semget()</a></i>,
<i><a href="sem_close.html">sem_close()</a></i>,
<i><a href="sem_destroy.html">sem_destroy()</a></i>,
<i><a href="sem_getvalue.html">sem_getvalue()</a></i>,
<i><a href="sem_init.html">sem_init()</a></i>,
<i><a href="sem_open.html">sem_open()</a></i>,
<i><a href="sem_post.html">sem_post()</a></i>,
<i><a href="sem_unlink.html">sem_unlink()</a></i>,
<i><a href="sem_wait.html">sem_wait()</a></i>,
<i><a href="sysipc.h.html">&lt;sys/ipc.h&gt;</a></i>,
<i><a href="syssem.h.html">&lt;sys/sem.h&gt;</a></i>,
<i><a href="systypes.h.html">&lt;sys/types.h&gt;</a></i>,
<a href="ipc.html">IPC</a>.
</blockquote><h4>DERIVATION</h4><blockquote>
Derived from Issue 2 of the SVID.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>

